package com.fjnldx.mes.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * 获取当前时间 所在周的 周一的 00:00 时间
     *
     * @return
     */
    public static Date getMonday(Date curDate) {
        if (null == curDate) {
            curDate = new Date();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(curDate);
        int a = cal.get(Calendar.DAY_OF_WEEK) - 1; // 得到今天是周几
        // 周几,如果是周日 变为7
        if (a == 0) {
            a = 7;
        }
        // 当前时间减去 几天得到周一的时间
        long resDateTime = curDate.getTime() - (a * 86400000);
        // 再加上1天的时间
        resDateTime += 86400000;
        // System.out.println(resDateTime);
        Date resDate = new Date(resDateTime);
        cal.setTime(resDate);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        resDate = cal.getTime();
        return resDate;
    }

    /**
     * 获取当前时间 所在周的 周一的 00:00 时间字符串
     *
     * @return
     */
    public static String getMondayStr(Date curDate) {
        Date monday = getMonday(curDate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(monday);
    }

    public static Date getMonth(Date curDate) {
        if (null == curDate) {
            curDate = new Date();
        }
        Date date = null;
        date = new Date(curDate.getYear(), curDate.getMonth(), 01);
        return date;
    }

    public static String getMonthStr(Date curDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = getMonth(curDate);
        return sdf.format(date);
    }



}
